package org.example.chat.entity;

import lombok.Data;
import org.example.chat.validate.annotation.XSS;
import org.example.chat.validate.groups.AddGroup;
import org.example.chat.validate.groups.UpdateGroup;
import org.hibernate.annotations.Comment;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.URL;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author cheval
 */
@Data
@Entity
@Table(name = "user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Null(message = "id必须为空", groups = {AddGroup.class})
    @NotEmpty(message = "id 不能为空", groups = {UpdateGroup.class})
    @Id
    @Column(name = "id", nullable = false, length = 64, updatable = false)
    private String id;

    @NotBlank(message = "账号不能为空", groups = {AddGroup.class})
    @Length(min = 8, max = 11, message = "账号长度必须在8 ~ 11", groups = {AddGroup.class})
    @Pattern(message = "账号必须是数字", regexp = "\\d{8,11}", groups = {AddGroup.class})
    @Null(message = "不能修改账号", groups = {UpdateGroup.class})
    @Column(name = "username", length = 50, nullable = false, unique = true, updatable = false)
    @Comment("账号")
    private String username;

    @NotBlank(message = "密码不能为空",groups = {AddGroup.class})
    @Length(min = 8, max = 30, message = "密码长度在8 ~ 30", groups = {AddGroup.class})
    @Pattern(regexp = "((\\d+[a-zA-Z]+)|([a-zA-Z]+\\d+))+", message = "密码必须是数字和字母的组合", groups = {AddGroup.class})
    @Null(message = "密码只能单独修改", groups = {UpdateGroup.class})
    @Column(name = "password", length = 150, nullable = false)
    @Comment("密码")
    private String password;

//    @XSS(groups = {AddGroup.class, UpdateGroup.class})
    @NotEmpty(message = "昵称不能为空", groups = {AddGroup.class})
    @Length(max = 50, message = "昵称最长50个字符", groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "nickname", length = 50, nullable = false)
    @Comment("昵称")
    private String nickname;

    @URL(message = "不符合http协议", groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "face_img")
    @Comment("聊天头像url")
    private String faceImg;

    @URL(message = "不符合http协议", groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "face_big_img")
    @Comment("用户信息头像url")
    private String faceBigImg;

    @URL(message = "不符合http协议", groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "qrcode", unique = true)
    @Comment("用户账号二维码url")
    private String qrCode;


    @Null(message = "创建日期不能为空", groups = {AddGroup.class})
    @Column(name = "create_date", updatable = false)
    @Comment("创建日期")
    private LocalDate createDate;

//    @ManyToMany(fetch = FetchType.LAZY)  // 多对多， 懒加载的方式获取数据值（调用get方法的时候才获取数据）
//    @JoinTable(name = "friends", // 多对多中间关联表名称
//        joinColumns = @JoinColumn(name = "user_id"), // 当前实体主键在关联表中的字段名称
//        inverseJoinColumns = @JoinColumn(name = "friend_id")  //  关联实体主键在关联表中的字段名称
//    )
//    private List<User> friends;

    @Column(name = "status")
    @Comment("用户状态, 0：正常 1: 锁定 2: 永久封禁， 默认0")
    private Integer status;

    @Email(message = "邮箱格式不正确", groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "email", length = 80)
    @Comment("邮箱")
    private String email;

    @Column(name = "sex")
    @Comment("性别, 0: 男 1: 女")
    private Integer sex;

    @Column(name = "join_method")
    @Comment("加好友方式，0： 直接加入， 1： 同意后加入, 默认1")
    private Integer joinMethod;

//    @XSS(groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "personalized_signature", length = 50)
    @Comment("个性签名")
    private String personalizedSignature;

    @Column(name = "last_login_time")
    @Comment("最后登录时间")
    private LocalDateTime lastLoginTime;

    @Column(name = "last_off_time")
    @Comment("最后离线时间")
    private LocalDateTime lastOffTime;

//    @XSS(groups = {AddGroup.class, UpdateGroup.class})
    @NotEmpty(message = "地区不能为空", groups = {AddGroup.class})
    @Column(name = "area_name", length = 120, nullable = false)
    @Comment("地区")
    private String areaName;

//    @XSS(groups = {AddGroup.class, UpdateGroup.class})
    @Column(name = "area_code", length = 50, nullable = false)
    @Comment("地区编号")
    private String areaCode;
}
